<template>
	<view class="view-contain">
		<public-module leftIcon="arrow-left" :title="title" />
		<!-- 训练类型选择 -->
		<view class="mode-selection">
			<view class="mode-cards">
				<view v-for="(mode, index) in trainingModes" :key="index" class="mode-card"
					@click="navigateToTraining(mode.path)">
					<view class="mode-icon">
						<u-icon :name="mode.icon" :color="mode.color" size="32"></u-icon>
					</view>
					<view class="mode-info">
						<text class="mode-title">{{ mode.title }}</text>
						<text class="mode-desc">{{ mode.description }}</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				title: '速算训练',
				// 训练模式
				trainingModes: [{
						title: '基础计算',
						description: '基础计算与估算训练',
						icon: 'edit-pen',
						color: '#FF9800',
						path: '/pages/calculation/estimation/index'
					},
					{
						title: '特殊计算',
						description: '尾数法、凑整法等高级计算技巧',
						icon: 'level',
						color: '#9C27B0',
						path: '/pages/calculation/special-calculation/index'
					},
					{
						title: '找数计算',
						description: '提升数据定位与分析能力',
						icon: 'search',
						color: '#4CAF50',
						path: '/pages/calculation/data-search/index?type=text' // 直接进入文字材料页面
					},
					{
						title: '资料分析',
						description: '图表分析与数据计算训练',
						icon: 'file-text',
						color: '#2196F3',
						path: '/pages/calculation/data-analysis/index'
					},
					{
						title: '一表通算',
						description: '资料一表通算，掌握表格数据计算',
						icon: 'grid',
						color: '#E91E63',
						path: '/pages/calculation/table-analysis/index'
					}
				]
			}
		},

		computed: {
			...mapState(['user', 'loginSuccess'])
		},

		methods: {
			...mapMutations(['emptyUserInfo']),
			// 导航到训练页面
			navigateToTraining(path) {
				// 检查是否是需要登录的训练路径且用户未登录
				if ((path.includes('estimation') || path.includes('special-calculation') || path.includes('data-analysis') || path.includes('table-analysis')) && !this.user?.userId) {
					// 静默登录机制，直接跳转到首页
					uni.reLaunch({
						url: '/pages/index/index'
					});
					return;
				}
				uni.navigateTo({
					url: path,
					fail: (err) => {
						console.error('导航失败:', err);
						uni.showToast({
							title: '页面跳转失败',
							icon: 'none'
						});
					}
				});
			},

			// 返回首页
			goBack() {
				uni.navigateBack();
			}
		}
	}
</script>

<style>
	/* 全局容器样式 */
	.container {
		display: flex;
		flex-direction: column;
		background-color: #f8f8f8;
		min-height: 100vh;
		box-sizing: border-box;
	}

	/* 安全区域适配刘海屏 */
	.safe-area {
		width: 100%;
		height: var(--status-bar-height);
		background-color: #f8f8f8;
	}

	/* 页面标题样式 */
	.header {
		width: 100%;
		padding: 12px 15px;
		display: flex;
		align-items: center;
		background-color: #ffffff;
		box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
		position: relative;
	}

	.back-icon {
		position: absolute;
		left: 15px;
	}

	.title {
		flex: 1;
		font-size: 18px;
		font-weight: 500;
		color: #333333;
		text-align: center;
	}

	/* 训练模式选择 */
	.mode-selection {
		width: 100%;
	}

	.mode-cards {
		display: flex;
		flex-direction: column;
		gap: 15px;
	}

	.mode-card {
		display: flex;
		align-items: center;	
		border-radius: 12px;
		padding: 20px;
		transition: all 0.3s ease;
	}

	.mode-card:active {
		transform: scale(0.98);
		box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
	}

	.mode-icon {
		width: 48px;
		height: 48px;
		border-radius: 12px;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-right: 16px;
	}

	.mode-info {
		flex: 1;
	}

	.mode-title {
		font-size: 18px;
		font-weight: 600;
		color: #333333;
		margin-bottom: 6px;
		display: block;
	}

	.mode-desc {
		font-size: 14px;
		color: #666666;
		line-height: 1.4;
	}
</style>
